//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;

namespace OpenLS.Chart.UI.Internals
{
    public class ScatterPanel : Panel
    {
        protected override Size MeasureOverride(Size availableSize)
        {
            var result = new Size();
            foreach (UIElement e in InternalChildren)
            {
                e.Measure(availableSize);
                result.Height = Math.Max(result.Height, e.DesiredSize.Height);
                result.Width = Math.Max(result.Width, e.DesiredSize.Width);
            }
            result = new Size(Math.Min(availableSize.Width, result.Width), Math.Min(availableSize.Height, result.Height));
            return result;

        }


        public static ScatterPanelPosition GetPosition(DependencyObject obj)
        {
            return (ScatterPanelPosition)obj.GetValue(PositionProperty);
        }

        public static void SetPosition(DependencyObject obj, ScatterPanelPosition value)
        {
            obj.SetValue(PositionProperty, value);
        }

        // Using a DependencyProperty as the backing store for Position.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PositionProperty =
            DependencyProperty.RegisterAttached("Position", typeof(ScatterPanelPosition), typeof(ScatterPanel), new FrameworkPropertyMetadata(ScatterPanelPosition.Center, FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsParentArrange ));


        
        protected override Size ArrangeOverride(Size finalSize)
        {
            ChartBase chart = ChartElement.GetChart(this);
            if (chart == null)
            {
                Debug.WriteLine("Problem with no chart");
                return finalSize;
            }
            var horizontalOrientation = HorizontalOrientation;
            var verticalOrientation = VerticalOrientation;

            var yProjector = new ValueAxisProjector(AxisInfoHelper.GetMin(ChartElement.GetYAxisInfo(this)),
                                                    AxisInfoHelper.GetMax(ChartElement.GetYAxisInfo(this)), finalSize.Height, 0,
                                                    verticalOrientation);
            double xMax = AxisInfoHelper.GetMax(ChartElement.GetXAxisInfo(this));
            double xMin = AxisInfoHelper.GetMin(ChartElement.GetXAxisInfo(this));
            var xProjector = new ValueAxisProjector(xMin,
                                                    xMax,  0,finalSize.Width,
                                                    horizontalOrientation); 
            foreach (FrameworkElement child in InternalChildren)
            {
                //double x = CategoryAxisPanel.GetScreenPosition(SeriesElement.GetIndex(child), 0, maxIndex, 0, finalSize.Width, horizontalOrientation, true); //\\for now

                var nv = (ScatterValue)child.DataContext;
                double y = yProjector.GetValue(nv.Y);
                double x = xProjector.GetValue(nv.X);
                var pt = new Point(x, y);

                var sz = child.DesiredSize;
                ScatterPanelPosition position = GetPosition(child);
                switch (position)
                {
                    case ScatterPanelPosition.Center:
                        pt.Offset(-sz.Width / 2, -sz.Height / 2);
                        break;
                    case ScatterPanelPosition.Left:
                        pt.Offset(-sz.Width , -sz.Height / 2);
                        break;
                    case ScatterPanelPosition.Right:
                        pt.Offset(0, -sz.Height / 2);
                        break;
                    case ScatterPanelPosition.Top:
                        pt.Offset(-sz.Width / 2, -sz.Height );
                        break;
                    case ScatterPanelPosition.Bottom:
                        pt.Offset(-sz.Width / 2, 0);
                        break;
                }
                child.Arrange(new Rect(pt, sz));
            }
            return finalSize;
        }




        public AxisOrientation VerticalOrientation
        {
            get { return (AxisOrientation)GetValue(VerticalOrientationProperty); }
            set { SetValue(VerticalOrientationProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ValueOrientation.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty VerticalOrientationProperty =
            DependencyProperty.Register("VerticalOrientation", typeof(AxisOrientation), typeof(ScatterPanel), new FrameworkPropertyMetadata(AxisOrientation.MinimumToMaximum, FrameworkPropertyMetadataOptions.AffectsArrange));



        public AxisOrientation HorizontalOrientation
        {
            get { return (AxisOrientation)GetValue(HorizontalOrientationProperty); }
            set { SetValue(HorizontalOrientationProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CategoryOrientation.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HorizontalOrientationProperty =
            DependencyProperty.Register("HorizontalOrientation", typeof(AxisOrientation), typeof(ScatterPanel), new FrameworkPropertyMetadata(AxisOrientation.MinimumToMaximum, FrameworkPropertyMetadataOptions.AffectsArrange));


    }
}